package inet.app.vod.activity.film;

import inet.app.vod.MyApp;
import inet.app.vod.activity.R;
import inet.app.vod.config.AppConstants;
import inet.app.vod.object.SubtitleSettingModel;
import inet.app.vod.util.BrightnessUtil;
import inet.app.vod.util.MyPreferenceManager;
import inet.app.vod.video.MyMediaControllerBase;
import inet.app.vod.view.MySpinner;
import inet.app.vod.view.MySpinner.MySpinerItemClickListener;
import afzkl.development.colorpickerview.dialog.ColorPickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class VideoSettingActivity extends FragmentActivity implements
		OnClickListener {

	final int TAB_MEDIA = 0;
	final int TAB_SUBTITLES = 1;
	final int MIN_BRIGHTNESS = 20;
	final int LOW_BRIGHTNESS = 70;
	final int NORMAL_BRIGHTNESS = 140;
	final int HIGHT_BRIGHTNESS = 210;

	Button mMediaTab;
	Button mSubTab;
	ViewFlipper mViewFlipper;

	// Media setting tab
	SeekBar mBrightnessSeekbar;
	Button mLowButton;
	Button mNormalButton;
	Button mHightButton;
	RadioGroup mMediaControllerRadioGroup;
	RadioButton mMediaControllerRadioButton;

	// Subtitle setting tab
	MySpinner mFontSpinner;
	TextView mSpinnerText;
	SeekBar mSizeSeekBar;
	TextView mSizeTextView;
	ImageButton mColorButton;
	CheckBox mBoldCheckbox;
	String[] mFontList;

	int mAppBrightness;
	int mSystemBrightness;

	MyApp mApplication;
	Context mContext;
	Resources mResources;

	SubtitleSettingModel mSubtitleSettingModel;
	ColorPickerDialog mColorDialog;

	private void initModels() {
		mApplication = (MyApp) getApplication();
		mContext = getApplicationContext();
		mResources = getResources();

		mSystemBrightness = BrightnessUtil
				.getSystemBrightness(getContentResolver());
		mAppBrightness = MyPreferenceManager.getInstance(mContext)
				.getBrightness();

		if (mAppBrightness < 0) {
			// no setting, set value brightness = system brightness
			mAppBrightness = mSystemBrightness;
		}

		mFontList = AppConstants.SUBTITLE_FONT;
		mSubtitleSettingModel = MyPreferenceManager.getInstance(mContext)
				.getSubtitleSetting();
	}

	/**
	 * Init media setting tab
	 * 
	 * @param view
	 */
	private void initMediaSettingViews() {

		mBrightnessSeekbar = (SeekBar) findViewById(R.id.brightness_seekbar);

		// Brightness of system from 0 to 255
		mBrightnessSeekbar.setMax(255);
		mBrightnessSeekbar.setProgress(mAppBrightness);

		mBrightnessSeekbar
				.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

					@Override
					public void onStopTrackingTouch(SeekBar seekBar) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onStartTrackingTouch(SeekBar seekBar) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onProgressChanged(SeekBar seekBar,
							int progress, boolean fromUser) {
						// TODO Auto-generated method stub
						if (fromUser) {
							setBrightness(progress);
						}
					}
				});

		mLowButton = (Button) findViewById(R.id.low_button);
		mLowButton.setOnClickListener(this);

		mNormalButton = (Button) findViewById(R.id.normal_button);
		mNormalButton.setOnClickListener(this);

		mHightButton = (Button) findViewById(R.id.hight_button);
		mHightButton.setOnClickListener(this);

		mMediaControllerRadioGroup = (RadioGroup) findViewById(R.id.controller_select_radio);
		int typeController = MyPreferenceManager.getInstance(mContext)
				.getTypeController();
		switch (typeController) {
		case MyMediaControllerBase.TYPE_BUTTON:
			mMediaControllerRadioGroup.check(R.id.controller_button_radio);
			break;
		case MyMediaControllerBase.TYPE_GESTURE:
			mMediaControllerRadioGroup.check(R.id.controller_gesture_radio);
			break;
		default:
			break;
		}
	}

	/**
	 * Init subtitle setting tab
	 * 
	 * @param view
	 */
	private void initSubtitleViews() {

		mFontSpinner = (MySpinner) findViewById(R.id.font_spinner);
		mFontSpinner.initData(this, getResources()
				.getString(R.string.chon_font), mFontList);
		mSpinnerText = (TextView) findViewById(R.id.font_text);
		mFontSpinner.setItemClickListener(new MySpinerItemClickListener() {

			@Override
			public void onItemClick(int position) {
				// TODO Auto-generated method stub
				mSpinnerText.setText(mFontList[position]);
			}
		});

		mSizeSeekBar = (SeekBar) findViewById(R.id.size_seekbar);
		mSizeTextView = (TextView) findViewById(R.id.size_number_textview);
		mColorButton = (ImageButton) findViewById(R.id.color_imagebutton);
		mBoldCheckbox = (CheckBox) findViewById(R.id.bold_checkbox);

		mSizeSeekBar
				.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

					@Override
					public void onProgressChanged(SeekBar seekBar,
							int progress, boolean fromUser) {
						if (fromUser) {
							mSizeTextView.setText(String.valueOf(progress));
						}

					}

					@Override
					public void onStartTrackingTouch(SeekBar seekBar) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onStopTrackingTouch(SeekBar seekBar) {
						// TODO Auto-generated method stub

					}
				});

		mColorButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				onClickColorPickerDialog();
			}
		});

		updateUISubtitleView();
	}

	/**
	 * Set value from preference to UI
	 */
	private void updateUISubtitleView() {
		int posFont = getIndexofOfElement(mFontList,
				mSubtitleSettingModel.getFont());
		mFontSpinner.setItemSelect(posFont);
		mSpinnerText.setText(mSubtitleSettingModel.getFont());
		mSizeSeekBar.setProgress(mSubtitleSettingModel.getSize());
		mSizeTextView.setText(String.valueOf(mSubtitleSettingModel.getSize()));
		mColorButton.setBackgroundColor(mSubtitleSettingModel.getColor());
		mBoldCheckbox.setChecked(mSubtitleSettingModel.isBold());
	}

	public int getIndexofOfElement(String[] array, String element) {
		if (array == null || array.length == 0) {
			return 0;
		}

		int i = 0;
		for (String str : array) {
			if (element.equals(str)) {
				return i;
			}
			i++;
		}
		return 0;

	}

	/**
	 * Save value from UI to preference
	 */
	private void saveSubtitlePreference() {

		mSubtitleSettingModel.setFont(mFontList[mFontSpinner.getItemSelect()]);
		mSubtitleSettingModel.setSize(Integer.valueOf(mSizeTextView.getText()
				.toString()));
		if (mColorDialog != null) {
			mSubtitleSettingModel.setColor(mColorDialog.getColor());
		}
		mSubtitleSettingModel.setBold(mBoldCheckbox.isChecked());
		MyPreferenceManager.getInstance(mContext).setSubtileSetting(
				mSubtitleSettingModel);
	}

	/**
	 * Init common view for all tabs
	 */
	private void initViews() {

		mViewFlipper = (ViewFlipper) findViewById(R.id.view_flipper);
		mMediaTab = (Button) findViewById(R.id.media_btn);
		mMediaTab.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				mViewFlipper.setDisplayedChild(0);
				resetTab(0);
			}
		});

		mSubTab = (Button) findViewById(R.id.sub_btn);
		mSubTab.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				mViewFlipper.setDisplayedChild(1);
				resetTab(1);
			}
		});

		initMediaSettingViews();
		initSubtitleViews();
	}

	/**
	 * Reset tab when click one yab
	 * 
	 * @param idTab
	 */
	private void resetTab(int idTab) {

		mViewFlipper.setDisplayedChild(idTab);

		// reset
		mMediaTab.setBackgroundColor(mResources.getColor(R.color.dark_gray));
		mMediaTab.setTextColor(mResources.getColor(R.color.gray_text));
		mSubTab.setBackgroundColor(mResources.getColor(R.color.dark_gray));
		mSubTab.setTextColor(mResources.getColor(R.color.gray_text));

		// set select
		switch (idTab) {
		case 0:
			mMediaTab.setBackgroundColor(mResources
					.getColor(R.color.light_gray));
			mMediaTab.setTextColor(mResources.getColor(R.color.white_text));
			break;
		case 1:
			mSubTab.setBackgroundColor(mResources.getColor(R.color.light_gray));
			mSubTab.setTextColor(mResources.getColor(R.color.white_text));
			break;
		}
	}

	/**
	 * Set brightness
	 */
	private void setBrightness(int brightness) {

		if (mApplication.mWindowBrightness == null) {
			Toast.makeText(mContext, "window brightness null",
					Toast.LENGTH_SHORT).show();
			return;
		}

		if (brightness < 0) {
			mAppBrightness = 0;
		} else if (brightness > 255) {
			mAppBrightness = 255;
		} else {
			mAppBrightness = brightness;
		}

		BrightnessUtil.setActivityBrightness(mApplication.mWindowBrightness,
				mAppBrightness);
	}

	/**
	 * Open dialog color picker
	 */
	public void onClickColorPickerDialog() {
		// The color picker menu item as been clicked. Show
		// a dialog using the custom ColorPickerDialog class.
		int initialValue = mSubtitleSettingModel.getColor();
		mColorDialog = new ColorPickerDialog(this, initialValue);
		mColorDialog.setAlphaSliderVisible(true);
		mColorDialog.setTitle("Pick a Color!");

		mColorDialog.setButton(DialogInterface.BUTTON_POSITIVE,
				getString(android.R.string.ok),
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						mSubtitleSettingModel.setColor(mColorDialog.getColor());
						updateUISubtitleView();
					}
				});

		mColorDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
				getString(android.R.string.cancel),
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// Nothing to do here.
					}
				});

		mColorDialog.show();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_video_setting_new);
		// hide the keyboard everytime the activty starts
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		initModels();
		initViews();
	}

	@Override
	public void onClick(View v) {

		boolean isSetBrightness = true;
		switch (v.getId()) {

		case R.id.low_button:
			mAppBrightness = LOW_BRIGHTNESS;
			break;
		case R.id.normal_button:
			mAppBrightness = NORMAL_BRIGHTNESS;
			break;
		case R.id.hight_button:
			mAppBrightness = HIGHT_BRIGHTNESS;
			break;
		default:
			isSetBrightness = false;
			break;
		}
		if (isSetBrightness) {
			updateUI();
			setBrightness(mAppBrightness);
		}
	}

	private void updateUI() {
		mBrightnessSeekbar.setProgress(mAppBrightness);
	}

	private void saveSetting() {

		MyPreferenceManager.getInstance(mContext).setBrightness(mAppBrightness);

		int id = mMediaControllerRadioGroup.getCheckedRadioButtonId();
		switch (id) {
		case R.id.controller_gesture_radio:
			MyPreferenceManager.getInstance(mContext).setTypeMediaController(
					MyMediaControllerBase.TYPE_GESTURE);
			break;
		case R.id.controller_button_radio:
			MyPreferenceManager.getInstance(mContext).setTypeMediaController(
					MyMediaControllerBase.TYPE_BUTTON);
			break;
		default:
			break;
		}

		saveSubtitlePreference();
	}

	@Override
	public void onStart() {
		super.onStart();
		/* init adapter here */
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onPause() {
		saveSetting();
		super.onPause();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		/* clear all data here */
		mApplication = null;
		mContext = null;
		mResources = null;
	}
}
